package org.adream.account.util;

import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import com.alibaba.fastjson.JSON;

public class FastJsonArgumentResolver implements HandlerMethodArgumentResolver {
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

		HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
		String paraName = parameter.getParameterAnnotation(FastJson.class).value();
		String jsonStr = request.getParameter(paraName);
		return JSON.parseObject(jsonStr,parameter.getParameterType());
	}

	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterAnnotation(FastJson.class) != null;

	}
}
